package com.techfansy.recyclerView;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.example.alltoollib.R;

import java.util.ArrayList;
import java.util.List;

public abstract class DeteleBaseAdapter<K, V> extends RecyclerView.Adapter {
    private List<DateTree<K, V>> data;
    protected List<Boolean> groupItemStatus;
    private int SubLayout = 0;

    public DeteleBaseAdapter() {

    }

    public DeteleBaseAdapter(List<DateTree<K, V>> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    //向外暴露设置显示数据的方法
    public void setData(List<DateTree<K, V>> dt) {
        this.data = dt;
        if (groupItemStatus == null) groupItemStatus = new ArrayList<>();
        else groupItemStatus.clear();
        initGroupItemStatus(groupItemStatus, data);
        notifyDataSetChanged();
    }

    /**
     * 增加组数据并且清除原先数据
     *
     * @param list
     */
    public void setLineData(List<K> list) {
        if (data == null) data = new ArrayList<>();
        else data.clear();
        for (int i = 0; i < list.size(); i++) {
            data.add(new DateTree<K, V>(list.get(i), null));
        }
        if (groupItemStatus == null) groupItemStatus = new ArrayList<>();
        else groupItemStatus.clear();
        initGroupItemStatus(groupItemStatus, data);
        notifyDataSetChanged();
    }

    /**
     * 增加组数据
     *
     * @param list
     */
    public void addLineData(List<K> list) {
        if (data == null) data = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            data.add(new DateTree<K, V>(list.get(i), null));
        }
        initGroupItemStatus(groupItemStatus, data);
        notifyDataSetChanged();
    }

    public List<K> getData() {
        List<K> list = null;
        if (list == null) list = new ArrayList();
        for (int i = 0; i < data.size(); i++) {
            list.add(data.get(i).getGroupItem());
        }
        return list;
    }

    public void addData(List<DateTree<K, V>> dt) {
        this.data.addAll(dt);
        initGroupItemStatus(groupItemStatus, dt);
        notifyDataSetChanged();
    }

    //设置初始值，所有 groupItem 默认为关闭状态
    private void initGroupItemStatus(List l, List<DateTree<K, V>> treeList) {
        for (int i = 0; i < treeList.size(); i++) {
            l.add(false);
        }
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        RecyclerView.ViewHolder vh = null;
        View v;
        if (i == ItemState.VIEW_TYPE_GROUPITEM) {
            v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_group_recyclerview, null, false);
            RelativeLayout relativeLayout = v.findViewById(R.id.relativeLayout);
            RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            v.setLayoutParams(lp);
            View childView = LayoutInflater.from(viewGroup.getContext()).inflate(setGroup_layout_id(), (ViewGroup) v.getParent(), true);
            RecyclerView.LayoutParams lp1 = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            childView.setLayoutParams(lp1);
            relativeLayout.addView(childView);
            vh = new GroupViewHolder(v);
        } else if (i == ItemState.VIEW_TYPE_SUBITEM && SubLayout != 0) {
            v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_sub_recyclerview, null, false);
            RelativeLayout relativeLayout = v.findViewById(R.id.relativeLayout);
            RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            v.setLayoutParams(lp);
            View childView = LayoutInflater.from(viewGroup.getContext()).inflate(SubLayout, (ViewGroup) v.getParent(), true);
            RecyclerView.LayoutParams lp1 = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            childView.setLayoutParams(lp1);
            relativeLayout.addView(childView);
            vh = new SubViewHolder(v);
        }
        return vh;
    }

    /**
     * 设置父删除显示样式
     */

    public void setGroupTextDelete(TextView textView, int position) {
    }

    /**
     * 设置子删除显示样式
     */

    public void setSubTextDelete(TextView textView, int position) {
    }


    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int i) {
        final ItemState itemStatus = getItemStatusByPosition(i);

        final DateTree<K, V> dt = data.get(itemStatus.getGroupItemIndex());

        if (itemStatus.getViewType() == ItemState.VIEW_TYPE_GROUPITEM) {

            final GroupViewHolder groupItemVh = (GroupViewHolder) viewHolder;

            //加载groupItem，处理groupItem控件
            setGroupItemBindHolder(itemStatus, groupItemVh);
            setGroupTextDelete((TextView) viewHolder.itemView.findViewById(R.id.tv_delete), i);
            groupItemVh.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int groupItemIndex = itemStatus.getGroupItemIndex();
                    if (!groupItemStatus.get(groupItemIndex)) {
                        groupClick(viewHolder,groupItemIndex,true);
                        //groupItem由“关闭”状态到“打开”状态

                        groupItemStatus.set(groupItemIndex, true);
                        if (dt.getList() != null)
                            notifyItemRangeInserted(groupItemVh.getAdapterPosition() + 1, dt.getList().size());

                    } else {
                        groupClick(viewHolder,groupItemIndex,false);
                        //groupItem由“打开”状态到“关闭”状态
                        groupItemStatus.set(groupItemIndex, false);
                        if (dt.getList() != null)
                            notifyItemRangeRemoved(groupItemVh.getAdapterPosition() + 1, dt.getList().size());
                    }
                }
            });
        } else if (itemStatus.getViewType() == ItemState.VIEW_TYPE_SUBITEM) {

            SubViewHolder subItemVh = (SubViewHolder) viewHolder;
            setSubTextDelete((TextView) viewHolder.itemView.findViewById(R.id.tv_delete), i);
            //加载subItem，处理subItem控件
            setSubItemBindHolder(itemStatus, subItemVh);
            subItemVh.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                }
            });
        }

    }
    //点击群组
    public void groupClick(RecyclerView.ViewHolder viewHolder,int position,boolean isCheck){}

    public abstract void setGroupItemBindHolder(ItemState itemStatus, RecyclerView.ViewHolder view);

    public void setSubItemBindHolder(ItemState itemState, SubViewHolder view) {

    }



    public abstract int setGroup_layout_id();

    public void setSub_layout_id(int layout) {
        this.SubLayout = layout;
        this.notifyDataSetChanged();
    }



    @Override
    public int getItemCount() {
        int count = 0;
        if (groupItemStatus == null || groupItemStatus.size() == 0) return 0;
        for (int i = 0; i < data.size(); i++) {
            if (groupItemStatus.get(i))

                count += (data.get(i).getList() != null ? data.get(i).getList().size() : 0) + 1;
            else count++;
        }
        return count;
    }

    @Override
    public int getItemViewType(int position) {

        return getItemStatusByPosition(position).getViewType();
    }

    private ItemState getItemStatusByPosition(int position) {

        ItemState itemStatus = new ItemState();

        int count = 0;    //计算groupItemIndex = i 时，position最大值
        int i = 0;

        //轮询 groupItem 的开关状态
        for (i = 0; i < groupItemStatus.size(); i++) {

            //pos刚好等于计数时，item为groupItem
            if (count == position) {
                itemStatus.setViewType(ItemState.VIEW_TYPE_GROUPITEM);
                itemStatus.setGroupItemIndex(i);
                break;

                //pos大于计数时，item为groupItem(i - 1)中的某个subItem
            } else if (count > position) {

                itemStatus.setViewType(ItemState.VIEW_TYPE_SUBITEM);
                itemStatus.setGroupItemIndex(i - 1);
                itemStatus.setSubItemIndex(position - (count - data.get(i - 1).getList().size()));
                break;

            }

            //无论groupItem状态是开或者关，它在列表中都会存在，所有count++
            count++;

            //当轮询到的groupItem的状态为“开”的话，count需要加上该groupItem下面的子项目数目
            if (groupItemStatus.get(i)) {
                if (data.get(i).getList() != null)
                    count += data.get(i).getList().size();

            }


        }

        //简单地处理当轮询到最后一项groupItem的时候
        if (i >= groupItemStatus.size()) {
            itemStatus.setGroupItemIndex(i - 1);
            itemStatus.setViewType(ItemState.VIEW_TYPE_SUBITEM);
            itemStatus.setSubItemIndex(position - (count - data.get(i - 1).getList().size()));
        }

        return itemStatus;
    }


    class GroupViewHolder extends RecyclerView.ViewHolder {
        public GroupViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    protected class SubViewHolder extends RecyclerView.ViewHolder {
        public SubViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
